package edu.njust.httpmanager.config;

import jakarta.annotation.Resource;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableConfigurationProperties(ApiConfigProperties.class)
public class ApiConfig {

    @Resource
    private ApiConfigProperties apiConfigProperties;

    @Bean
    public OkHttpClient httpClient() {

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .writeTimeout(apiConfigProperties.getWriteTimeout(), TimeUnit.MILLISECONDS)
                .readTimeout(apiConfigProperties.getReadTimeout(), TimeUnit.MILLISECONDS)
                .callTimeout(apiConfigProperties.getCallTimeout(), TimeUnit.MILLISECONDS)
                .connectTimeout(apiConfigProperties.getConnectTimeout(), TimeUnit.MILLISECONDS)
                .build();
    }


}
